package test;

import java.util.ArrayList;
import java.util.List;

public class VolatileTest2 {

    volatile List<Integer> testList = new ArrayList<>();

    public static void main(String[] args) {
        VolatileTest2 volatileTest2 = new VolatileTest2();
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                volatileTest2.getTestList().add(i);
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread thread2 = new Thread(() -> {
            while (true){
                if (volatileTest2.getTestList().size() == 5){
                    thread1.interrupt();
                    break;
                }
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        thread2.start();
        thread1.start();

        System.out.println(volatileTest2.getTestList().size());
    }

    public List<Integer> getTestList() {
        return testList;
    }
}
